<?php

/**
 *
 * $Rev:: 6             	$:  Revision of last commit
 * $Author:: guy        	$:  Author of last commit
 * $Date:: 2010-01-06 1#	$:  Date of last commit
 *
 */

require_once './inc/config.inc.php';

function __autoload($class_name) 
{
    require_once "./lib/".$class_name.".class.php";
}

if (isset($_GET['displayRule'])) {
	return "$$('#ruledisplay').html(\"<div class=\"predicateboxes\">\" + $$('#1').html() + \"</div><div class=\"clear\"></div>\")";
}

if (isset($_GET['manage'])) {
	$manage = $_GET['manage'];
	if (isset($_GET['id'])){
		$myManage = new $manage($_GET['id']);
	} else {
		$myManage = new $manage();
	}
}

if (isset($_POST['addCourse'])) {

	if($myManage->addCourse($_POST['addCourse'])) {
		print highlightText("Course Added");
	} else {
		header("HTTP/1.0 400 Bad Request");
	}
	exit;
}

if (isset($_POST['removeCourse'])) {

	if ($myManage->removeCourse($_POST['removeCourse'])) {
		print highlightText("Course Removed");
	} else {
		header("HTTP/1.0 400 Bad Request");
	}
	exit;
}

include_once './inc/header.inc.php';

if (!isset($_GET['action'])) {

	?>
	<dl>
		<dt><a href="<?php print $_SERVER['PHP_SELF'];?>?manage=Rule&amp;action=view">Rules</a></dt>
		<dd>Manage Rules</dd>
		<dt><a href="<?php print $_SERVER['PHP_SELF'];?>?manage=Predicate&amp;action=view">Predicates</a></dt>
		<dd>Manage Predicates</dd>
		<dt><a href="view.php">View</a></dt>
		<dd>View All Student registrations</dd>
	</dl>
	</body>
</html>
	<?php
	
	die;
}
?>
	
<h1>Admin <?php print $_GET['action'] ." ". $manage ?></h1>

<?php

switch ($_GET['action']) {
	case "view":
		print $myManage->getTable("html");
		print "<p><a href=\"{$_SERVER['PHP_SELF']}?manage=$manage&amp;action=add\">add</a></p>";
	break;
	case "add":
		print $myManage->add();
		break;
	case "edit":
		print $myManage->edit($_GET['id']);
		break;
	case "delete":
		$result = $myManage->delete($_GET['id']);
		print header("location:{$_SERVER['PHP_SELF']}?manage=$manage&action=view&".  ( $result ? http_build_query($result):"error=An error occured") );

		break;
	case "insert":
		$result = $myManage->insert($_POST);
		print header("location:{$_SERVER['PHP_SELF']}?manage=$manage&action=view&". ( $result ? http_build_query($result):"error=An error occured"));

		break;
	case "update":
		$result = $myManage->update($_GET['id'],$_POST);
		print header("location:{$_SERVER['PHP_SELF']}?manage=$manage&action=view&". ( $result ? http_build_query($result):"error=An error occured") );
		break;
	default:

		?>
		<dl>
			<dt><a href="<?php print $_SERVER['PHP_SELF'];?>?manage=Rule&amp;action=view">Rules</a></dt>
			<dd>Manage Rules</dd>
			<dt><a href="<?php print $_SERVER['PHP_SELF'];?>?manage=Predicate&amp;action=view">Predicates</a></dt>
			<dd>Manage Predicates</dd>
		</dl>
		
		<?php
		
		break;
}

?>
<script type="text/javascript" src="js/admin.js" />
</body>
</html>
